package com.csr;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

public class MenuActivity extends Activity 
{
	Button btnBack,btnLogOut;
	GridView menu;
	
	ArrayList<Integer> servicesImage;
	ArrayList<String> servicesName;
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        btnLogOut=(Button)findViewById(R.id.btnLogout); 
		btnBack=(Button)findViewById(R.id.btnPrevious);
		
		menu=(GridView)findViewById(R.id.menuView);
		btnBack.setVisibility(Button.INVISIBLE);

		servicesName=new ArrayList<String>();
		servicesImage=new ArrayList<Integer>();
		
		servicesName.add("Agent");
		servicesName.add("Distributor");
		servicesName.add("Channel owner");
		servicesName.add("My Account");
		servicesName.add("Settings");
		
		servicesImage.add(R.drawable.agent);
		servicesImage.add(R.drawable.distributor);
		servicesImage.add(R.drawable.ch_owner);
		servicesImage.add(R.drawable.my_account);
		servicesImage.add(R.drawable.help);
		
		menu.setAdapter(new ImageAdapter(this,servicesName,servicesImage));
		
		 btnLogOut.setOnClickListener(new OnClickListener()
		 {
			
			public void onClick(View v)
			{
				  new AlertDialog.Builder(MenuActivity.this)
			        .setTitle( "Exit Application" )
			        .setMessage( "Are you sure you want to Logout?" )
			        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
			        {
			            public void onClick(DialogInterface arg0, int arg1) 
			            {
			                //do stuff onclick of YES
			                finish();
			            }

						public void onClick(View v) 
						{
							// TODO Auto-generated method stub
							
						}
			        })
			        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() {
			            public void onClick(DialogInterface arg0, int arg1)
			            {
			                //do stuff onclick of CANCEL
			            	arg0.dismiss();
			               
			            }

						public void onClick(View v) 
						{
							// TODO Auto-generated method stub
						}
			        }).show();
		  	}
			
		 });
        
    }
	@Override
	public void onBackPressed() 
	{
		// TODO Auto-generated method stub
		
		new AlertDialog.Builder(MenuActivity.this)
        .setTitle( "Exit Application." )
        .setMessage( "Are you sure you want to Exit ?" )
        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface arg0, int arg1) 
            {
                //do stuff onclick of YES
            	finish();
            }

			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				
			}
        })
        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1)
            {
                //do stuff onclick of CANCEL
            	arg0.dismiss();
               
            }

			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
			}
        }).show();
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
	    if(resultCode==2 && requestCode==11)
	    {
	        finish();
	    }
	}

	protected AlertDialog createDialog(CharSequence[] items, final String nextScreen) 
	{
		AlertDialog.Builder builder=new AlertDialog.Builder(MenuActivity.this);
		
		builder.setItems(items, new DialogInterface.OnClickListener() 
		{
			
			public void onClick(DialogInterface dialog, int position) 
			{
		
				if(position==0)
				{
					startActivityForResult(new Intent(MenuActivity.this,NewReseller.class).putExtra("nextscreen", nextScreen),11);
				}
				else
				{
					startActivityForResult(new Intent(MenuActivity.this,ManageAgentActivity.class).putExtra("nextscreen", nextScreen),11);
				}
				
			}
		});
		AlertDialog alert=builder.create();
		
		return alert;
		
	}
	public class ViewHolder
	{
			public TextView textView;
			public Button buttonView; 
	
    }
	public class ImageAdapter extends BaseAdapter 
	{
	    private Context mContext;
	     
	    ArrayList<String> services;
	    ArrayList<Integer> images;
	    AlertDialog alert;
	 
		public ImageAdapter(Context c,
				ArrayList<String> servicesName, ArrayList<Integer> servicesImage)
		{
			mContext = c;
			this.services=servicesName;
			this.images=servicesImage;
	    	 
		}
	 
		public int getCount() 
	    {
	        return services.size();
	    }

	    public Object getItem(int position) 
	    {
	        return null;
	    }

	    public long getItemId(int position) 
	    {
	        return position;
	    }
	    
	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(final int position, View convertView, ViewGroup parent) 
	    { 
	    	View v=convertView;
	    	ViewHolder holder=null;
	    	if(v==null)
	    	{
	    		holder=new ViewHolder();
	    		 LayoutInflater li = getLayoutInflater();
	    		 v=li.inflate(R.layout.grid_item, null);
	    		 holder.buttonView=(Button)v.findViewById(R.id.grid_item_image);
	    		 holder.textView=(TextView)v.findViewById(R.id.grid_item_text);
	    		 v.setTag(holder);
	    	}
	    	else
	    		holder=(ViewHolder)v.getTag();
	    		holder.textView.setText(services.get(position));			
	    		holder.buttonView.setBackgroundResource(images.get(position));
	    		holder.buttonView.setOnClickListener(new OnClickListener() 
		        {
	    			
					public void onClick(View v) 
					{
						if(position==0)
						{
							alert=createDialog(new CharSequence[]{"New Agent","Manage Agent"},"Agent");
							alert.show();
						}
						else if(position==1)
						{
							alert=createDialog(new CharSequence[]{"New Distributor","Manage Distributor"},"Distributor");
							alert.show();
						}
						else if(position==2)
						{
							alert=createDialog(new CharSequence[]{"New Channel Owner","Manage Channel Owner"},"Channel Owner");
							alert.show();
						}
						else  if(position==3)
						{
							startActivityForResult(new Intent(MenuActivity.this,MyAccountActivity.class),11);
						}
						else if(position==4)
						{
							startActivityForResult(new Intent(MenuActivity.this,SettingsActivity.class),11);
						}
					}
				});
			return v; 
	    }
	    
	}
	/*public class ImageAdapter extends BaseAdapter 
	{
	    private Context mContext;
	     
	    ArrayList<String> services;
	    ArrayList<Integer> images;
	    AlertDialog alert;
	 
		public ImageAdapter(Context c,
				ArrayList<String> servicesName, ArrayList<Integer> servicesImage)
		{
			mContext = c;
			this.services=servicesName;
			this.images=servicesImage;
	    	 
		}

		public int getCount() 
	    {
	        return services.size();
	    }

	    public Object getItem(int position) 
	    {
	        return null;
	    }

	    public long getItemId(int position) 
	    {
	        return position;
	    }

	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(final int position, View convertView, ViewGroup parent) 
	    { 
	    	
	        Button buttonView;
	        if (convertView == null)
	        {  // if it's not recycled, initialize some attributes
	        	buttonView = new Button(mContext);
	        	buttonView.setLayoutParams(new GridView.LayoutParams(110, 110)); 
	        	buttonView.setPadding(8, 8, 8, 8);
	        }
	        else
	        {
	        	buttonView = (Button) convertView;
	        }
	        
	        //buttonView.setBackgroundResource(services[position]);
	        buttonView.setCompoundDrawablesWithIntrinsicBounds(0, images.get(position), 0,0);
	        buttonView.setText(services.get(position));
	         
	        buttonView.setBackgroundColor(android.graphics.Color.TRANSPARENT);
	        buttonView.setTextColor(android.graphics.Color.WHITE);
	        
	        buttonView.setOnClickListener(new OnClickListener() 
	        {
				
				public void onClick(View v) 
				{
					if(position==0)
					{
						alert=createDialog(new CharSequence[]{"New Agent","Manage Agent"},"Agent");
						alert.show();
					}
					else if(position==1)
					{
						alert=createDialog(new CharSequence[]{"New Distributor","Manage Distributor"},"Distributor");
						alert.show();
					}
					else if(position==2)
					{
						alert=createDialog(new CharSequence[]{"New Channel Owner","Manage Channel Owner"},"Channel Owner");
						alert.show();

					}
					else  if(position==3)
					{
						startActivityForResult(new Intent(MenuActivity.this,MyAccountActivity.class),11);
						
					}
					else if(position==4)
					{
						startActivityForResult(new Intent(MenuActivity.this,SettingsActivity.class),11);
					} 
				}
			});
	        
	        return buttonView;
	        
	    }
	    
	}*/
}
